﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Media.Animation;

namespace MyAnimelistRecommender
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Random r = new Random();

        public MainWindow()
        {
            try
            {
                if (Properties.Settings.Default.SaveState != null)
                {
                    App.Model = Properties.Settings.Default.SaveState.Model;
                    DataHelper.RecommendationDataHelper.LoadData(this, App.Model, true);
                }
            }
            catch (Exception) { }
            this.DataContext = App.Model;
            if (App.Model.AnimeList.Count == 0)
            {
                DataHelper.RecommendationDataHelper.LoadData(this, App.Model);
            }
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataHelper.RecommendationDataHelper.CalcBestAnime(App.Model);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DataHelper.RecommendationDataHelper.LoadData(this, App.Model);
        }

        private void SearchTextBox_Search(object sender, RoutedEventArgs e)
        {
            DataHelper.RecommendationDataHelper.CalcBestAnime(App.Model);
        }

        //private void lbWeighted_ScrollChanged(object sender, ScrollChangedEventArgs e)
        //{
        //    lbNormal.GetDescendantByType<ScrollViewer>().ScrollToVerticalOffset(e.VerticalOffset);
        //}
        private void Storyboard_Completed(object sender, EventArgs e)
        {
            this.Hide();
        }

        //private void lbNormal_ScrollChanged(object sender, ScrollChangedEventArgs e)
        //{
        //    lbWeighted.GetDescendantByType<ScrollViewer>().ScrollToVerticalOffset(e.VerticalOffset);
        //}
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            return;
            e.Cancel = true;
            mainGrid.Height = mainGrid.ActualHeight;
            mainGrid.Width = mainGrid.ActualWidth;
            ((Storyboard)mainGrid.Resources["EndOfStory"]).Begin();
            var t = new Thread(() =>
            {
                var splash = new MyAnimelistRecommender.SplashScreen();
                splash.ShowDialog();
            });
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            new Thread(() =>
            {
                DataHelper.RecommendationDataHelper.Save(App.Model);
                Dispatcher.InvokeShutdown();
            }).Start();
            this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.Cursor.Clip = new System.Drawing.Rectangle((int)this.Top, (int)this.Left, (int)this.ActualWidth, (int)this.ActualHeight);
        }
    }
}